package elasticsearch.util;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.ClusterAdminClient;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.health.ClusterIndexHealth;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;

/**
 * 索引增删改查
 * @author jiangbang
 * @date 2017/7/27
 */
public class ESAdminClientUtil {
    private static final String index = "index1";
    private static final String index2 = "index2";
    private static final String type = "type1";
    private static final String type2 = "type2";
    /**
     * 创建索引并设置setting
     * @param client
     */
    public static void createIndex(TransportClient client) {
        AdminClient adminClient = client.admin();
        IndicesAdminClient indicesAdminClient = adminClient.indices();
        indicesAdminClient.prepareCreate(index2)
                .setSettings(Settings.builder()
                        .put("index.number_of_shards", 3)
                        .put("index.number_of_replicas", 1)
                ).get();
    }
    /**
     * 创建索引并设置类型
     * @param client
     */
    public static void createIndexandType(TransportClient client) {
        AdminClient adminClient = client.admin();
        IndicesAdminClient indicesAdminClient = adminClient.indices();
        indicesAdminClient.prepareCreate(index2)
                .addMapping("type2", "{\n" +
                        "    \"type2\": {\n" +
                        "      \"properties\": {\n" +
                        "        \"message\": {\n" +
                        "          \"type\": \"string\"\n" +
                        "        }\n" +
                        "      }\n" +
                        "    }\n" +
                        "  }")
                .get();
    }

    /**
     * 创建类型
     * @param client
     */
    public static void setType(TransportClient client) {
        client.admin().indices().preparePutMapping(index2)
                .setType(type2)
                .setSource("{\n" +
                        "  \"properties\": {\n" +
                        "    \"name\": {\n" +
                        "      \"type\": \"string\"\n" +
                        "    }\n" +
                        "  }\n" +
                        "}")
                .get();
    }

    /**
     * 刷新索引
     * @param client
     */
    public static void refresh (TransportClient client) {
        //刷新所有索引
        client.admin().indices().prepareRefresh().get();
        //刷新单个索引
        client.admin().indices()
                .prepareRefresh(index)
                .get();
        //刷新多个索引
        client.admin().indices()
                .prepareRefresh(index, index2)
                .get();
    }

    /**
     * 获取索引设置
     * @param client
     */
    public static void getSetting (TransportClient client) {
        GetSettingsResponse response = client.admin().indices()
                .prepareGetSettings(index, index2).get();
        for (ObjectObjectCursor<String, Settings> cursor : response.getIndexToSettings()) {
            String index = cursor.key;
            Settings settings = cursor.value;
            Integer shards = settings.getAsInt("index.number_of_shards", null);
            Integer replicas = settings.getAsInt("index.number_of_replicas", null);
        }
    }
    /**
     * 修改索引设置
     * @param client
     */
    public static void updateSetting (TransportClient client) {
        client.admin().indices().prepareUpdateSettings(index2)
                .setSettings(Settings.builder()
                        .put("index.number_of_replicas", 2)
                )
                .get();
    }

    /**
     * 集群状态
     * @param client
     */
    public static void clusterHealth (TransportClient client) {
        //获取集群
        ClusterAdminClient clusterAdminClient = client.admin().cluster();
        //获取集群健康信息（所有的指标信息）
        ClusterHealthResponse healths = clusterAdminClient.prepareHealth().get();
        //获取集群名称
        String clusterName = healths.getClusterName();
        //获取数据节点的总数
        int numberOfDataNodes = healths.getNumberOfDataNodes();
        //获取节点总数
        int numberOfNodes = healths.getNumberOfNodes();

        for (ClusterIndexHealth health : healths.getIndices().values()) {
            //索引名称
            String index = health.getIndex();
            //索引分区数量
            int numberOfShards = health.getNumberOfShards();
            //索引副本数量
            int numberOfReplicas = health.getNumberOfReplicas();
            //索引状态
            ClusterHealthStatus status = health.getStatus();
        }
    }
    public static void clusterWaitStatus (TransportClient client) {
        client.admin().cluster().prepareHealth()
                .setWaitForYellowStatus()
                .get();
        client.admin().cluster().prepareHealth(index)
                .setWaitForGreenStatus()
                .get();

        client.admin().cluster().prepareHealth(index2)
                .setWaitForGreenStatus()
                .setTimeout(TimeValue.timeValueSeconds(2)) //最多等待2秒
                .get();
    }





}
